package org.example;

import java.util.Comparator;
import java.util.PriorityQueue;

class Imp implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2 - o1;
    }
}

class Solution {
    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if (k <= 0 || arr == null || arr.length == 0) {
            return new int[0];
        }
        PriorityQueue<Integer> maxHeap = new PriorityQueue(k, new Imp());
        for (int i = 0; i < k; i++) {
            maxHeap.offer(arr[i]);
        }
        for(int i = k; i < arr.length; i++) {
            int top = maxHeap.peek();
            if (arr[i] < top) {
                maxHeap.poll();
                maxHeap.offer(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = maxHeap.poll();
        }
        return ret;

    }
}